package cn.rongdao.jrkabao.base;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;

import com.igexin.sdk.PushManager;
import com.igexin.sdk.PushService;
import com.mifengkong.frtools.http.request.callback.BaseImpl;
import com.mifengkong.frtools.http.request.callback.FRBaseObserver;
import com.mifengkong.frtools.ui.activity.FRBaseActivity;
import com.mifengkong.frtools.util.FRDeviceUtil;
import com.mifengkong.frtools.util.FRSharedPreferences;
import com.mifengkong.frtools.util.FRString;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.rongdao.jrkabao.bean.cashadvance.HomeBean;
import cn.rongdao.jrkabao.constant.SPConstant;
import cn.rongdao.jrkabao.model.cashcoupon.SendTaskCouponModel;
import cn.rongdao.jrkabao.ui.setting.DialogCheckUpdatePresenter;
import cn.rongdao.jrkabao.util.CommonUtil;
import cn.rongdao.jrkabao.util.dialog.DialogKaMaoPresenter;
import cn.rongdao.jrkabao.util.dialog.DialogPresenter;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 *
 */

public abstract class BaseActivity extends FRBaseActivity implements BaseImpl {

    private CompositeDisposable disposablesByDestroy;// 管理Destroy取消订阅者者

    protected abstract void initView();
    private String uniqueId = "";

    private DialogPresenter dialogPresenter;
    private DialogCheckUpdatePresenter dialogCheckUpdatePresenter;

    private Unbinder unbinder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        unbinder=ButterKnife.bind(this);
        PushManager.getInstance().initialize(this.getApplicationContext(), PushService.class);

        if (null == dialogPresenter) {
            dialogPresenter = new DialogPresenter(thisActivity());
        }
        if (null == disposablesByDestroy) {
            disposablesByDestroy = new CompositeDisposable();
        }
        initView();
    }

    protected void initUniqueId() {
        uniqueId = FRSharedPreferences.getStringDefault(SPConstant.UNIQUEID, "");
        if (TextUtils.isEmpty(uniqueId)) {
            try {
                uniqueId = FRDeviceUtil.getPhoneInfo();
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (TextUtils.isEmpty(uniqueId)) {
                uniqueId = CommonUtil.getUUID();
            }
            FRSharedPreferences.setStringDefault(SPConstant.UNIQUEID, uniqueId);
        }
    }

    public String getUniqueId() {
        return uniqueId;
    }

    public void showLoanConsultDialog() {
        if (null != dialogPresenter) {
            dialogPresenter.show();
        }
    }

    public void showDialogCheckUpdatePresenter() {
        if (null == dialogCheckUpdatePresenter) {
            dialogCheckUpdatePresenter = new DialogCheckUpdatePresenter(thisActivity());
        }
        dialogCheckUpdatePresenter.show();
    }

    public void showKaMaoDialog(String imageUrl, String link) {
        if (FRString.isEmpty(imageUrl)){
            return;
        }
        DialogKaMaoPresenter dialogKaMaoPresenter = new DialogKaMaoPresenter(thisActivity(), imageUrl, link);
        dialogKaMaoPresenter.show();
    }

    @Override
    public void addRxDestroy(Disposable disposable) {
        if (null != disposablesByDestroy) {
            disposablesByDestroy.add(disposable);
        }
    }

    public void sendTaskCoupon() {
        SendTaskCouponModel.getInstance().execute(new FRBaseObserver<HomeBean.PopUpInfoBean>(this) {
            @Override
            protected void onBaseNext(HomeBean.PopUpInfoBean data) {
                if (CommonUtil.isStatusSuccess(data.getIs_show())) {
                    showKaMaoDialog(data.getImg_url(), data.getLink_url());
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != disposablesByDestroy) {
            disposablesByDestroy.dispose();
            disposablesByDestroy = null;
        }
        if (null!=unbinder){
            unbinder.unbind();
        }
    }

    @Override
    public Activity getActivity() {
        return this;
    }
}